﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

namespace LabMS.Web.Role
{
    public partial class Edit : Base.BasePage
    {
        ExtendBLL.RoleTable EBrt = new ExtendBLL.RoleTable();
        protected void Page_Load(object sender, EventArgs e)
        {
            strPagePrivilege.Add("JSGL");
            strPageUserType.Add("mana");
            if (!IsPostBack)
            {
                if (Request.QueryString["RoleId"] == null)
                {
                    Common.JShelper.JSAlert(this.Page, "", "你无权查看此页面！");
                }
                else
                {
                    int RoleId = int.Parse(Request.QueryString["RoleId"].ToString());
                    InitBind(RoleId);
                }
            }
            int otherRoleId = int.Parse(Request.QueryString["RoleId"].ToString());
            List<LabMS.Model.RoleFucntion> listRF = new List<LabMS.Model.RoleFucntion>();
            LabMS.BLL.RoleFucntion bRF = new LabMS.BLL.RoleFucntion();
            listRF = bRF.GetModelList(" RoleID = " + otherRoleId.ToString());
            CreatePrivailyPanel(listRF);            
        }

        /// <summary>
        /// 页面初始化
        /// </summary>
        /// <param name="Rolename"></param>
        protected void InitBind(int RoleId)
        {
            string str = "RoleId="+RoleId;
            tbRoleName.Text=EBrt.GetModelList(str)[0].RoleName;
            tbRoleDetails.Text = EBrt.GetModelList(str)[0].RoleDetails;
        }
        
        /// <summary>
        /// 保存修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(tbRoleName.Text.Trim()))
            {
                Common.JShelper.JSAlert(this.Page, "err", "角色名不能为空！");
                tbRoleName.Focus();
            }
            else if (string.IsNullOrEmpty(tbRoleDetails.Text.Trim()))
            {
                Common.JShelper.JSAlert(this.Page, "err", "角色描述不能为空！");
            }
            else
            {
                LabMS.BLL.RoleTable BRT = new LabMS.BLL.RoleTable();
                LabMS.Model.RoleTable Mrt = new LabMS.Model.RoleTable();
                int RoleId = int.Parse(Request.QueryString["RoleId"].ToString());
                Mrt.RoleId = RoleId;
                Mrt.RoleName = EBrt.GetRoleNameByRoleID(RoleId);
                Mrt.RoleName = tbRoleName.Text.Trim();
                Mrt.RoleDetails = tbRoleDetails.Text.Trim();
                try
                {
                    BRT.Update(Mrt);
                    ExtendBLL.RoleFucntion erf = new ExtendBLL.RoleFucntion();
                    erf.delbyWhere("RoleID=" + RoleId.ToString());

                    List<string> liststrFunction = GetPrivaily();
                    LabMS.BLL.RoleFucntion brf = new LabMS.BLL.RoleFucntion();
                    LabMS.Model.RoleFucntion mrf = new LabMS.Model.RoleFucntion();
                    for (int i = 0; i < liststrFunction.Count; ++i)
                    {
                        mrf.FCode = liststrFunction[i].ToString();
                        mrf.RoleID = RoleId;
                        brf.Add(mrf);
                    }

                    //Common.JShelper.JSAlert(this.Page, "", "修改角色成功！");
                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script type='text/javascript'>alert('修改角色成功！');window.location.href='RoleList.aspx';</script>");
                }
                catch
                {

                }
            }
        }

        List<string> GetPrivaily()
        {

            LabMS.BLL.Function bFuntion = new LabMS.BLL.Function();
            List<LabMS.Model.Function> listPFuntion = new List<LabMS.Model.Function>();
            List<string> liststrFunction = new List<string>();
            listPFuntion = bFuntion.GetModelList("");
            for (int i = 0; i < listPFuntion.Count; ++i)
            {
                try
                {
                    CheckBox check = (CheckBox)PRolePrivaily.FindControl("check" + listPFuntion[i].Code);
                    if (check.Checked)
                    {
                        liststrFunction.Add(listPFuntion[i].Code);
                    }
                }
                catch
                {
                    ;
                }
            }
            return liststrFunction;

        }

      

        void CreatePrivailyPanel(List<LabMS.Model.RoleFucntion> listRF)
        {
            lbErr.Visible = false;

            List<LabMS.Model.Plate> listPPlate = new List<LabMS.Model.Plate>();
            List<LabMS.Model.Module> listPModule = new List<LabMS.Model.Module>();
            List<LabMS.Model.Function> listPFuntion = new List<LabMS.Model.Function>();

            LabMS.BLL.Plate bPlate = new LabMS.BLL.Plate();
            LabMS.BLL.Module bModule = new LabMS.BLL.Module();
            LabMS.BLL.Function bFuntion = new LabMS.BLL.Function();

            try
            {
                listPPlate = bPlate.GetModelList("");
            }
            catch (Exception ex)
            {
                lbErr.Visible = true;
                lbErr.Text = ex.Message;
                return;
            }
            PRolePrivaily.Controls.Add(new LiteralControl("<ul class='plate'>"));
            foreach (LabMS.Model.Plate p in listPPlate)
            {
                PRolePrivaily.Controls.Add(new LiteralControl("<li id='" + p.Code + "'>"));

                PRolePrivaily.Controls.Add(new LiteralControl(p.Name));
                try
                {
                    listPModule = bModule.GetModelList("PCode = '" + p.Code + "'");
                }
                catch (Exception ex)
                {
                    lbErr.Visible = true;
                    lbErr.Text = ex.Message;
                    return;
                }
                PRolePrivaily.Controls.Add(new LiteralControl("<ul class='module'>"));
                foreach (LabMS.Model.Module m in listPModule)
                {
                    PRolePrivaily.Controls.Add(new LiteralControl("<li id='" + m.Code + "'>"));

                    CheckBox check = new CheckBox();
                    check.ID = "chk" + m.Code.ToString();
                    check.Text = m.Name.ToString();
                    check.CssClass = "module";
                    check.Attributes.Add("onclick", "checkAllChanged('chk" + m.Code.ToString() + "','div" + m.Code + "')");
                    PRolePrivaily.Controls.Add(check);
                    PRolePrivaily.Controls.Add(new LiteralControl("<div id='div" + m.Code + "'>"));
                    try
                    {
                        listPFuntion = bFuntion.GetModelList("MCode = '" + m.Code + "'");
                    }
                    catch (Exception ex)
                    {
                        lbErr.Visible = true;
                        lbErr.Text = ex.Message;
                        return;
                    }
                    foreach (LabMS.Model.Function f in listPFuntion)
                    {
                        bool ischeck = false;
                        for (int j = 0; j < listRF.Count; ++j)
                        {
                            if (listRF[j].FCode == f.Code)
                            {
                                ischeck = true;
                            }
                        }

                        check = new CheckBox();
                        check.ID = "check" + f.Code.ToString();
                        check.Text = f.Name.ToString();
                        check.Checked = ischeck;
                        check.CssClass = "function";
                        check.Attributes.Add("onclick", "checkFunctionChanged('chk" + m.Code.ToString() + "','div" + m.Code + "')");
                        PRolePrivaily.Controls.Add(check);
                    }
                    PRolePrivaily.Controls.Add(new LiteralControl("</div >"));
                    PRolePrivaily.Controls.Add(new LiteralControl("</li>"));
                }
                PRolePrivaily.Controls.Add(new LiteralControl("</ul>"));

                PRolePrivaily.Controls.Add(new LiteralControl("</li>"));
            }
            PRolePrivaily.Controls.Add(new LiteralControl("</ul>"));
        }
    }
}
